package com.sinochem.sales.service.service.impl;


import com.sinochem.sales.common.api.to.sms.SendMsg;
import com.sinochem.sales.common.api.vo.SendSmsinfoVO;
import com.sinochem.sales.service.gateways.SendSmsService;
import com.sinochem.sales.service.service.IMessageTemplateService;
import com.sinochem.sales.service.service.ISendSmsinfoService;
import org.apache.log4j.Logger;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.utils.DateUtil;
import org.sagacity.sqltoy.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xuan  <a href="zhangshixuanj@163.com">联系作者</a>
 * @version Revision:v1.0,Date: 2018/1/11 19:58
 * @project sinochem-parent
 * @description 发送短信
 * @Modification Date: 2018/1/11 19:58  {填写修改说明}
 */
@Service
public class SendSmsinfoServiceImpl implements ISendSmsinfoService {
    /**
     * 日志
     */
    private static Logger logger = Logger.getLogger(SendSmsinfoServiceImpl.class);

    @Autowired
    private SqlToyLazyDao sqlToyLazyDao;

    @Autowired
    private IMessageTemplateService messageTemplateService;

    @Autowired
    private SendSmsService sendSmsService;

    @Override
    public boolean sendSmsinfo(SendSmsinfoVO sendSmsinfoVO) {

        if (!StringUtil.isNotNullAndBlank(sendSmsinfoVO.getMobile())) {
            logger.error("sendSmsinfo-->发送短信的参数对象为空! sendSmsinfoVO=" + sendSmsinfoVO);
            return false;
        }

        if (!StringUtil.isNotNullAndBlank(sendSmsinfoVO.getEnterpriseId())) {
            sendSmsinfoVO.setEnterpriseId("无");//如果企业ID为空,设置为默认值
        }

        if (!StringUtil.isNotNullAndBlank(sendSmsinfoVO.getMobile())) {
            logger.error("sendSmsinfo-->手机号为空! sendSmsinfoVO=" + sendSmsinfoVO);
            return false;
        }

        //获取替换的文本
        String text = null;
        try {
            text = messageTemplateService.messageTemplateContents(sendSmsinfoVO.getModel(), sendSmsinfoVO.getMessageType());
            logger.info("发送短信内容---text=>" + text);
            if (StringUtil.isNullOrBlank(text)) {
                logger.error("sendSmsinfo-->查询短信模版为空! sendSmsinfoVO=" + sendSmsinfoVO);
                return false;
            }
        } catch (Exception e) {
            logger.error("sendSmsinfo-->查询短信模版异常! sendSmsinfoVO=" + sendSmsinfoVO, e);
            return false;
        }

        try {
            //添加纪录
            // 通道发送状态 0成功、1失败；业务发送状态【1.成功 2.失败】
            //sendSmsinfoVO.setSendState("1".equals(rsp.getStrReturnCode())?"1":"2");
            sendSmsinfoVO.setContents(text);
            //sendSmsinfoVO.setCode(rsp.getSendMsgResult());
            add(sendSmsinfoVO);
        } catch (Exception e) {
            logger.error("sendSmsinfo-->add 保存短信异常! sendSmsinfoVO=" + sendSmsinfoVO, e);
        }

        try {
            //发送短信
            SendMsg sendMsg = new SendMsg();
            sendMsg.setStrContent(text);
            sendMsg.setStrMobileNo(sendSmsinfoVO.getMobile());
            sendSmsService.sendSmsMessage(sendMsg);
            logger.info("sendSmsinfo------------>发送短信完成!");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("sendSmsinfo-->add-->发送短信失败!", e);
        }

        return false;
    }

    @Override
    public String add(SendSmsinfoVO sendSmsinfoVO) {
        try {
            //发送时间
            sendSmsinfoVO.setSendTime(DateUtil.getNowTime());
            return (String) this.sqlToyLazyDao.save(sendSmsinfoVO);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("SendSmsinfoServiceImpl-->add-->添加操作失败!", e);
        }
        return null;
    }

}
